package cn.neusoft.controller;

import cn.neusoft.pojo.LoginUser;
import cn.neusoft.vo.ResultBean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {
    @PostMapping("/in")
    public ResultBean login(HttpSession session, @RequestBody LoginUser loginUser, ServletRequest servletRequest) {
        System.out.println("进入登录页面");
        //登录成功分配id
        if("admin".equals(loginUser.getUsername()) && "123456".equals(loginUser.getPassword())) {
            System.out.println("登录成功："+session.getId());
            //这里为什么进行将loginUser分配给这个呢
            session.setAttribute("adminUser",loginUser);
            return ResultBean.SUCCESS("admin");
        }
        return ResultBean.ERROR("登录失败，用户名或密码错误");
    }
    @GetMapping("/out")     // get + /log/out
    public ResultBean logout(HttpSession session){
        session.removeAttribute("adminUser");
        return ResultBean.SUCCESS();
    }
}
